﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using Fly.Common.Linq;
using Newtonsoft.Json;
using System.Web.Services;
using System.Web.UI.HtmlControls;

namespace RN.ERP.Web.CA
{
    public partial class CAProductSelect : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidEmpId.Value = Request.QueryString["EmpId"];
                string name = HttpUtility.UrlDecode(Request.QueryString["Name"]);
                string idCard = HttpUtility.UrlDecode(Request.QueryString["IDCard"]);
                string proCode = Request.QueryString["ProCode"];
                string group = Request.QueryString["group"];
                hidGroup.Value = group;
                hidAreaCode.Value = UserInfo.CurrentArea;
                OrderServiceClient client = new OrderServiceClient();
                //hidEmpId.Value = Request.QueryString["EmpId"];
                //string name = Request.QueryString["Name"];
                //string idCard = Request.QueryString["IDCard"];
                //string proCode = Request.QueryString["ProCode"];
                lbName.Text = name;
                lbIdCard.Text = idCard;
                if (hidGroup.Value == "CA03" || hidGroup.Value == "CA02")//人员已购买
                {
                    List<ModelService.tb_ProductInfo> product = JsonConvert.DeserializeObject<List<ModelService.tb_ProductInfo>>(client.GetProductsByUserGuid(hidEmpId.Value, UserInfo.CurrentArea, 1));
                    repProduct.DataSource = product;
                    repProduct.DataBind();
                    for (int i = 0; i < repProduct.Items.Count; i++)
                    {
                        Label lbYearlyFee = (Label)repProduct.Items[i].FindControl("lbYearlyFee");
                        Label lbAddFee = (Label)repProduct.Items[i].FindControl("lbAddFee");
                        lbAddFee.Text = (double.Parse(lbYearlyFee.Text) + 100).ToString();
                    }
                }
                else
                {
                    ModelService.ProductCondition condition = new ModelService.ProductCondition();
                    if (UserInfo.RoleType == "Manager")
                    {
                        condition = new ModelService.ProductCondition()
                        {
                            Code = new ParameterValue<string>()
                            {
                                V = "D",
                                O =
                                    OptEnum.StartWith
                            },
                            GroupCode = new ParameterValue<string>() { V = group, O = OptEnum.Equal }
                        };
                    }
                    else if (UserInfo.RoleType == "User")
                    {
                        condition = new ModelService.ProductCondition()
                        {
                            Code = new ParameterValue<string>()
                            {
                                V = "F",
                                O =
                                    OptEnum.StartWith
                            },
                            GroupCode = new ParameterValue<string>() { V = group, O = OptEnum.Equal }
                        };
                    }
                    List<ModelService.ProductCondition> query = new List<ModelService.ProductCondition>() { condition };
                    string queryStr = JsonConvert.SerializeObject(query);
                    List<ModelService.tb_ProductInfo> product = JsonConvert.DeserializeObject<List<ModelService.tb_ProductInfo>>(client.GetProducts(queryStr,
                        UserInfo.CurrentArea));
                    repProduct.DataSource = product;//绑定人员产品列表
                    repProduct.DataBind();
                }
                if (!string.IsNullOrEmpty(proCode)) //修改
                {
                    for (int j = 0; j < repProduct.Items.Count; j++)
                    {
                        HtmlInputCheckBox cbox = (HtmlInputCheckBox)repProduct.Items[j].FindControl("ckChoose");
                        Label lbCode = (Label)repProduct.Items[j].FindControl("lbCode");
                        if (lbCode.Text == proCode)
                        {
                            cbox.Checked = true;
                        }
                    }
                }
            }
        }

        [WebMethod]
        public static string GetInfomation(object proid, object areacode)
        {
            string pro = null;
            OrderServiceClient client = new OrderServiceClient();
            int id = int.Parse(proid.ToString());
            ModelService.ProductCondition condition = new ModelService.ProductCondition() { Id = new ParameterValue<int>() { V = id, O = OptEnum.Equal } };
            List<ModelService.ProductCondition> query = new List<ModelService.ProductCondition>() { condition };
            string queryStr = JsonConvert.SerializeObject(query);
            List<ModelService.tb_ProductInfo> list = JsonConvert.DeserializeObject<List<ModelService.tb_ProductInfo>>(client.GetProducts(queryStr,
                areacode.ToString()));
            if (list.Count > 0)
            {
                ModelService.tb_ProductInfo product = list.FirstOrDefault();//获取产品信息
                pro = JsonConvert.SerializeObject(new { Code = product.Code });
            }
            return pro;
        }
    }
}